package com.wugh.fast.security.core.dto;

import com.alibaba.fastjson.JSONObject;
import com.wugh.fast.security.core.util.AuthorityUtils;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-10-18 16:39
 */
public class JSONUserDetails extends JSONObject implements FastUserDetails {
    Collection<? extends GrantedAuthority> authorities;

    @Override
    public String getNickname() {
        return getString("nickname");
    }

    @Override
    public String getUserId() {
        String userId = getString("userId");
        return userId == null ? getString("id") : userId;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.getAuthorities(this);
    }

    @Override
    public String getPassword() {
        return getString("password");
    }

    @Override
    public String getUsername() {
        return getString("username");
    }

    @Override
    public boolean isAccountNonExpired() {
        return getBoolean("isAccountNonExpired", "accountNonExpired");
    }

    @Override
    public boolean isAccountNonLocked() {
        return getBoolean("isAccountNonLocked", "accountNonLocked");
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return getBoolean("isCredentialsNonExpired", "credentialsNonExpired");
    }

    @Override
    public boolean isEnabled() {
        return getBoolean("isEnabled", "enable");
    }

    private boolean getBoolean(String key, String otherKey) {
        Boolean result = getBoolean(key);
        Boolean otherResult = getBoolean(otherKey);
        return result != null ? result : otherResult != null ? otherResult : false;
    }
}
